import { getlogin,changeImg,changeuser } from '../../api/user'
const state = {
  username: '',
  userid: '',
  userimg: '',
  userinfo: ''
}
const mutations = {
  SET_USERINFO(state, payload) {
    state.username = payload.username
    state.userid = payload.id
    state.userimg = payload.img
    state.userinfo = payload.info
  },
  SET_IMG(state,img){
    state.userimg =img
  }
}
const actions = {
  async login(store, params) {
    let res = await getlogin(params)
    console.log(res);
    if (res.data.length) {
      store.commit('SET_USERINFO', res.data[0])
      return { code: 200 }
    } else {
      return { code: 201 }
    }
  },
  // 上传图片
  async upload(store,file,config) {
    let res = await changeImg(file,config)   
      store.commit('SET_IMG', res.data.url)
      return res
  },
  // 修改个人信息
  async changeUser(store,data){
    let res = await changeuser(data,store.state.userid)
    if(res.status==200){
      store.commit('SET_USERINFO',res.data)
    }
    return res
  }
}

export default {
  state, mutations, actions
}